package com.atguigu.ggkt.vod.controller;

import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vod.service.VodService;
import com.atguigu.ggkt.vod.utils.ConstantYmlUtil;
import com.atguigu.ggkt.vod.utils.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2022/7/1 15:36
 */
@RestController
@RequestMapping("/admin/vod")
@Api(tags = "视频点播管理接口")
public class VodController {
    @Autowired
    private VodService vodService;

    /**
     * 上传视频
     * @return
     */
    @ApiOperation("上传视频")
    @PostMapping("video")
    @CrossOrigin
    public Result upload() {
        String fileId = vodService.uploadVideo();
        return Result.ok(fileId);
    }

    /**
     * 删除视频
     * @param fileId 文件id
     * @return
     */
    @ApiOperation("删除视频")
    @DeleteMapping("video/{fileId}")
    public Result delete(@PathVariable String fileId) {
        vodService.deleteVideo(fileId);
        return Result.ok();
    }

    /**
     * 获取签名
     * @return
     */
    @ApiOperation("获取签名")
    @GetMapping("video/sign")
    public Result sign() {
        Signature sign = new Signature();
        // 设置App的云 API 密钥
        sign.setSecretId(ConstantYmlUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantYmlUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
            System.out.println("signature : " + signature);
            return Result.ok(signature);
        } catch (Exception e) {
            System.out.print("获取签名失败");
            e.printStackTrace();
            return Result.fail();
        }
    }
}
